package com.thz.system.service.course.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.thz.system.mapper.blog.BlogMapper;
import com.thz.system.mapper.course.CourseMapper;
import com.thz.system.mapper.registration.CourseRegistrationMapper;
import com.thz.system.mapper.video.VideoMapper;
import com.thz.system.pojo.blog.Blog;
import com.thz.system.pojo.course.Course;
import com.thz.system.pojo.registration.CourseRegistration;
import com.thz.system.pojo.video.Video;
import com.thz.system.service.course.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * @Description 课程服务实现类
 * @Author thz
 * @Date 2025/5/8
 */
@Service
public class CourseServiceImpl implements ICourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private CourseRegistrationMapper courseRegistrationMapper;

    @Override
    public List<Course> list() {
        return courseMapper.selectList(null);
    }

    @Override
    public Course selectCourseById(Long id) {
        return courseMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertCourse(Course course) {
        return courseMapper.insert(course);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateCourse(Course course) {
        return courseMapper.updateById(course);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteCourse(Long id) {
//        List<Blog> blogs =  blogMapper.selectList(new LambdaQueryWrapper<Blog>()
//                .select(Blog::getBlogId)
//                .eq(Blog::getCourseId, id));
//        for (Blog blog : blogs) {
//            blogMapper.deleteById(blog.getBlogId());
//        }

        blogMapper.delete(new LambdaQueryWrapper<Blog>()
                .eq(Blog::getCourseId, id));

        videoMapper.delete(new LambdaQueryWrapper<Video>()
                .eq(Video::getCourseId, id));

        courseRegistrationMapper.delete(new LambdaQueryWrapper<CourseRegistration>()
                .eq(CourseRegistration::getCourseId, id));

//        List<Video> voids =  videoMapper.selectList(new LambdaQueryWrapper<Video>()
//                .select(Video::getVideoId)
//                .eq(Video::getCourseId, id));
//        for (Video video : voids) {
//            videoMapper.deleteById(video.getVideoId());
//        }

//        List<CourseRegistration> courseRegistrations = courseRegistrationMapper.selectList(new LambdaQueryWrapper<CourseRegistration>()
//                .select(CourseRegistration::getCourseRegistrationId)
//                .eq(CourseRegistration::getCourseId, id));
//
//        for (CourseRegistration courseRegistration : courseRegistrations) {
//            courseRegistrationMapper.deleteById(courseRegistration.getCourseRegistrationId());
//        }
        return courseMapper.deleteById(id);
    }


}
